<?php
require_once 'Dto.php';
require_once 'Dao.php';

class Bus{
    
    protected $_name;
    protected $db;
    protected $dao;
    
    public function __construct(DataProvider $dbObj,$tablename="") {
        $this->db = $dbObj;
        if(!empty($tablename)) {
            $this->_name = $tablename;
        }
        $this->dao = new Dao($this->_name, $dbObj);
        $this->_init();
    }
    
    protected function _init() {
        
    }
    
    public function insert($data) {
        if(is_array($data)) {
            $data = new Dto( $this->_name, $this->db, $data);
        }
        $id = $this->dao->insert($data);
        return $id;
    }
    
    public function delete($where) {
        return $this->dao->delete($where);
    }
    
    public function update($data, $where) {
        if(is_array($data)) {
            $data = new Dto($this->_name,$this->db,$data);
        }
        return $this->dao->update($data, $where);
    }
    public function select($param = array('fields'=>array(),'where'=>'','order'=>'','limit'=>'')) {
        return $this->dao->select($param);
    }
    
    public function getAllData(){
        return $this->dao->select();
    }
    
    public function getItemsPager($start, $offset){
        return $this->select(array(
            'limit' => "{$start}, {$offset}"
        ));
    }
    
    
    public function countTotalRow(){
        return $this->dao->countTotalRow();
    }
    public function __destruct(){
        
    }
}
?>